/**
 * Created by apple on 2018/4/22.
 */

requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {

    var url = {
        addDocUrl: "manager/insertDocService.do",
        listUrl: 'application/document_template/document_template.html',
        docDetails: 'manager/selectDocId.do',
        queryProduct: 'manager/selectProductAllList.do',
        listEscrow: 'manager/listEscrow.do',
        selectLoanMethodList: "manager/selectLoanMethodList.do"
    };

    //渲染搜索框的贷款方式
    site.send({
        url: url.selectLoanMethodList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#loanMethod_list_tpl").html();
                var template = Handlebars.compile(source);
                var list = data
                var html = template(list)
                $('.loanWay_select').append(html);
            }
        }
    })

    // 查询产品
    site.send({
        url: url.queryProduct,
        data: {},
        callback: function (data) {
            var rs = JSON.parse(data);
            var scrous = $("#queryProduct_content_tpl").html();
            var template = Handlebars.compile(scrous);
            $("#queryProduct_content").html(template(rs));

            if (rs.type == 1) {

            } else {
                Constant.fn.Message('F', rs.msg);
            }
        }
    })

// 查询资方
    site.send({
        url: url.listEscrow,
        data: {pageNum: 1},
        callback: function (data) {
            var rs = JSON.parse(data);
            var scrous = $("#escrow_content_tpl").html();
            var template = Handlebars.compile(scrous);
            $("#escrow_content").html(template(rs));


            if (rs.type == 1) {

            } else {
                Constant.fn.Message('F', rs.msg);
            }
        }
    })
    //初始化编辑器
    var editor;

    var initEditor = function () {
        editor = window.UE.getEditor('msg_cont');

        $('.D-messg-span span').on('click', function () {
            var placehold = $(this).data('placehold');
            editor.focus();
            editor.execCommand('inserthtml', placehold);//'{'+placehold+'}'
        });

    }

    if (window.UE && window.UE.version) {
        initEditor();
    } else {
        setTimeout(initEditor, 200);
    }

    //初始化编辑器 end


    var id = getAurlparam('id');

    $("#doc_type").on("change", function () {
        var flag = true;
        var type = $(this).val();
        if ($(this).val()) {
            $.each($('.choose-per'), function (i, e) {
                //console.log()
                //console.log($(e).data('userid'));
                if (type == $(e).data('type')) {
                    //$('.chooseRole .selPers').append('<span data-userid= ' + userId + ' class="choose-per"><span class="choose-perName">' + userName + '</span><span class="removeRole" style="cursor: pointer;">×</span></span>');
                    flag = false;
                }
            })
            if (flag) {
                $(this).parents('.input-title-first').append('<span data-type="' + $(this).val() + '" class="choose-per"><span class="choose-perName">' + $(this).find('option:selected').text() + '</span><span class="removeRole" style="cursor: pointer;">×</span></span>')
            }
            initDel();
            $(this).val('');
        }


    })

    function initDel(){
        $('.removeRole').unbind().on('click', function () {
            $(this).parents('.choose-per').remove();
        })
    }

    if (id) {
        //通过id获取数据
        site.send({
            url: url.docDetails,
            data: {id: id},
            callback: function (data) {
                var list = JSON.parse(data);
                if (list.type == 1) {
                    // $("#doc_type").val(-1);
                    $("#doc_name").val(list.content.doc_name);
                    //$("#doc_type option[value=" + list.content.doc_type + "]").prop('selected', true);
                    var doc_type = list.content.doc_type.split(',');
                    if(doc_type){
                        $.each(doc_type, function (i, e) {
                            var text = $("#doc_type option[value=" + e + "]").text()
                            $('.doc_type').append('<span data-type="' + e + '" class="choose-per"><span class="choose-perName">' + text +'</span><span class="removeRole" style="cursor: pointer;">×</span></span>')
                        })
                        initDel();
                    }
                    $("#product_id option[value=" + list.content.product_id + "]").prop('selected', true);
                    $("#management_id option[value=" + list.content.management_id + "]").prop('selected', true);
                    var con = list.content.doc_content;


                    /*editor.on("ready",function(){
                        editor.setContent(con);
                    })*/
                    setTimeout(function () {//加延迟用来显示内容
                        editor.setContent(con);
                    }, 500);

                    /*editor.addListener("ready", function () {
                        // editor准备好之后才可以使用
                        editor.setContent('abc');
                    });*/

                } else {
                    Constant.fn.Message('F', list.msg);
                }
            }
        })
    }

    //添加事件
    $('.btn-change').on('click', function () {
        var param = {doc_content: editor.getContent()};
        Constant.fn.formParam('#add_doc_div', param);


        if (id) {
            param.id = id;
            url.addDocUrl = 'manager/updateDocService.do';
        }
        var doc_type = "";
        $.each($('.doc_type .choose-per'), function (i, e) {
            doc_type += $(e).data('type') + ','
        })
        doc_type = doc_type.substring(0, doc_type.length - 1);
        param.doc_type = doc_type;
        var verify = {
            name: {
                value: param.doc_type,
                name: '贷款方式',
                isNeed: true
            },
            product: {
                value: param.product_id,
                name: '选择产品',
                isNeed: true
            },
            escrow: {
                value: param.management_id,
                name: '选择资方',
                isNeed: true
            },
            code: {
                value: param.doc_name,
                name: '文档名称',
                isNeed: true
            },
            contact: {
                value: param.doc_content,
                name: '文档内容',
                isNeed: true
            }
        };
        if (!Constant.fn.validator(verify)) {
            return;
        }
        site.send({
            url: url.addDocUrl,
            data: param,
            callback: function (data) {
                var rs = JSON.parse(data);
                if (rs.type == 1) {
                    Constant.fn.Message('T', rs.msg);
                    pjaxGo(url.listUrl);
                } else {
                    Constant.fn.Message('F', rs.msg);
                }
            }
        })
    });


//
});